package com.sphd.restaurantsystem.activitys.order;

import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.AddOrderPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.AddOrderView;
import com.wzh.frame.utils.MyUtils;
import com.wzh.frame.utils.StringUtils;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddordersActivity extends BaseActivity implements AddOrderView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.addorders_time)
    TextView addordersTime;
    @BindView(R.id.addorders_table)
    MaterialEditText addordersTable;
    @BindView(R.id.addorders_money)
    MaterialEditText addordersMoney;
    @BindView(R.id.addorders_user_phone)
    MaterialEditText addordersUserPhone;
    @BindView(R.id.up_date)
    Button upDate;
    // 定义显示时间控件
    private Calendar calendar; //通过Calendar获取系统时间
    private int mHour;
    private int mMinute;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    AddOrderPresenterImpl addOrderPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addorders);
        addActivity(this);
        ButterKnife.bind(this);
        initView();
        intEvent();
    }

    private void intEvent() {
        toolbar.setNavigationOnClickListener(view -> {
            finishActivity(this);
        });
    }

    private void initView() {
        setTitleBar(toolbar, "录入订单");
        intent = getIntent();
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        calendar = Calendar.getInstance();
        addOrderPresenter = new AddOrderPresenterImpl(this);
        addordersTime.setText(calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));
    }


    @OnClick({R.id.addorders_time, R.id.up_date})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.addorders_time:
                addTime();
                break;
            case R.id.up_date:
                checkAll();
                break;
        }
    }

    private void checkAll() {
        if (StringUtils.isEmpty(addordersTable.getText().toString().trim())) {
            addordersTable.setError("请输入桌号");
        } else {
            if (StringUtils.isEmpty(addordersMoney.getText().toString())) {
                addordersMoney.setError("请输入金额");
            } else {
                if (isNum(addordersMoney.getText().toString())) {
                    if (Double.parseDouble(addordersMoney.getText().toString()) <= 0) {
                        addordersMoney.setError("金额必须大于0");
                    } else {
                        if (StringUtils.isEmpty(addordersUserPhone.getText().toString())) {
                            addordersUserPhone.setError("手机号码不能为空");
                        } else {
                            if (!MyUtils.isMobileNO(addordersUserPhone.getText().toString())) {
                                addordersUserPhone.setError("请输入正确的手机号码");
                            } else {
                                myNet();
                            }
                        }
                    }
                }
            }
        }
    }

    private void myNet() {
        String url = RestaurantUrl.Httptitle + RestaurantUrl.AddOrders
                + "cgid=" + restaurantInfo.getId()
                + "&sum=" + addordersMoney.getText().toString().trim()
                + "&tableno=" + addordersTable.getText().toString().trim()
                + "&phone=" + addordersUserPhone.getText().toString()
                + "&time=" +
                calendar.get(Calendar.YEAR) + "-" +
                ((calendar.get(Calendar.MONTH) + 1) < 10 ? "0" +
                        (calendar.get(Calendar.MONTH) + 1) : (calendar.get(Calendar.MONTH) + 1))
                + "-" + calendar.get(Calendar.DAY_OF_MONTH) +
                "%20" + addordersTime.getText().toString().trim();
        addOrderPresenter.addData(url);
    }

    private void addTime() {
        new TimePickerDialog(context,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hour, int minute) {
                        // TODO Auto-generated method stub
                        mHour = hour;
                        mMinute = minute;
                        //更新EditText控件时间 小于10加0
                        addordersTime.setText(new StringBuilder()
                                .append(mHour < 10 ? "0" + mHour : mHour).append(":")
                                .append(mMinute < 10 ? "0" + mMinute : mMinute));
                    }
                }, calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), true).show();
    }

    @Override
    public void recOk() {
        toast("录入订单成功");
        finishActivity(this);
    }

    @Override
    public void recFaile() {
        toast("录入订单失败，请重试！");
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
